package com.code.petapp.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.code.petapp.R;
import com.code.petapp.bean.FosterBean;
import com.code.petapp.databinding.ActivityFosterEditBinding;
import com.code.petapp.http.HttpListener;
import com.code.petapp.util.FosterUtils;
import com.code.petapp.util.SPCommonUtils;

import java.util.Calendar;
import java.util.Objects;

public class FosterEditActivity extends BaseActivity {
private ActivityFosterEditBinding binding;
    private Calendar calendar = Calendar.getInstance();
    private int id;
    private int userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityFosterEditBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setCalendar();
        Intent intent = getIntent(); // 获取 Intent
        String name = intent.getStringExtra("name"); // 获取 name
        String time = intent.getStringExtra("time"); // 获取 time
        String days = intent.getStringExtra("days"); // 获取 days
        id = intent.getIntExtra("id",0);  // 获取 id
        userId=SPCommonUtils.getInstance().getUserId();
        binding.name.setText(name);
        binding.time.setText(time);
        binding.days.setText(days);
        binding.save.setOnClickListener(view -> {
            if(binding.name.getText().toString().isEmpty()||binding.time.getText().toString().isEmpty()||binding.days.getText().toString().isEmpty()){
                toastShortShow("请完善信息");
            }else{
                setSave();
            }
        });

    }
    @SuppressLint({"DefaultLocale", "SetTextI18n"})
    private void setCalendar() {
        int currentYear = calendar.get(Calendar.YEAR);
        int currentMonth = calendar.get(Calendar.MONTH) + 1;
        int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
        binding.calendarView.setOnPageChangedCallBack((year, month) -> {
            binding.calendarTitle.setText(year + "-" + month);
        });
        binding.calendarView.setOnDateSelectCallBack((year, month, day) -> {
            if (year < currentYear ||
                    (year == currentYear && month < currentMonth) ||
                    (year == currentYear && month == currentMonth && day < currentDay)) {
                // 如果选择的日期无效，可以给出提示
                toastShortShow("无法选择当前日期之前的日期");
                binding.time.setText("");
                return;
            }
            binding.time.setText(String.format("%04d-%02d-%02d", year, month, day));
        });
    }
    private void setSave(){
        String name = binding.name.getText().toString();
        String time = binding.time.getText().toString();
        String days = binding.days.getText().toString();
        FosterBean fosterBean=new FosterBean();
        fosterBean.setName(name);
        fosterBean.setTime(time);
        fosterBean.setDays(days);
        fosterBean.setStatus("待审核");
        fosterBean.setUserId(userId);
        fosterBean.setId(id);
        FosterUtils.update(SPCommonUtils.getInstance().getToken(),fosterBean, new HttpListener<>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void success(String data) {
                toastShortShow("保存成功");
                startActivity(new Intent(FosterEditActivity.this,FosterActivity.class));
                finish();
            }

            @Override
            public void error(int code, String error) {
                toastShortShow(error);
            }
        });
    }
}